package com.nkl.page.action;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.nkl.common.util.JSONData;
import com.nkl.common.util.PaperUtil;
import com.nkl.page.domain.Activity;
import com.nkl.page.domain.College;
import com.nkl.page.domain.Member;
import com.nkl.page.domain.News;
import com.nkl.page.domain.Picnews;
import com.nkl.page.domain.Sblog;
import com.nkl.page.domain.User;
import com.nkl.page.manager.IndexManager;

@Controller
public class IndexAction {

	@Autowired
	IndexManager indexManager;
	String tip;
	
	public IndexManager getIndexManager() {
		return indexManager;
	}

	public void setIndexManager(IndexManager indexManager) {
		this.indexManager = indexManager;
	}
	
	@RequestMapping(value="page_index.action",method=RequestMethod.GET)
	public String index(ModelMap model){
		try {
			//查询图片新闻
			List<Picnews> picnewss = indexManager.listPicnews(new Picnews());
			model.addAttribute("picnewss", picnewss);
			
			//查询社团新闻资讯前9条
			News news = new News();
			news.setStart(0);
			news.setLimit(8);
			//news.setNews_type(1);
			news.setNews_flag(2);
			List<News> newss1 = indexManager.listNewss(news,null); 
			model.addAttribute("newss1", newss1);
			
			//查询社团简介
			College college = new College();
			college.setStart(0);
			college.setLimit(10);
			college.setCollege_flag(2);
			List<College> colleges = indexManager.listColleges(college,null);
			model.addAttribute("colleges", colleges);
			
			//查询社团活动
			Activity activity = new Activity();
			activity.setStart(0);
			activity.setLimit(5);
			activity.setActivity_type(1);
			activity.setActivity_flag(2);
			List<Activity> activitys1 = indexManager.listActivitys(activity,null);
			model.addAttribute("activitys1", activitys1);
			
			//查询校园活动
			activity.setActivity_type(2);
			List<Activity> activitys2 = indexManager.listActivitys(activity,null);
			model.addAttribute("activitys2", activitys2);
			
			//查询社团新闻
			news.setNews_type(1);
			news.setStart(0);
			news.setLimit(5);
			List<News> newss2 = indexManager.listNewss(news,null); 
			model.addAttribute("newss2", newss2);
			
			//查询校园新闻
			news.setNews_type(2);
			news.setStart(0);
			news.setLimit(5);
			List<News> newss3 = indexManager.listNewss(news,null); 
			model.addAttribute("newss3", newss3);
			
			//查询留言信息
			Sblog sblog = new Sblog();
			sblog.setStart(0);
			sblog.setLimit(5);
			sblog.setSblog_flag(2);
			List<Sblog> sblogs = indexManager.listSblogs(sblog,null); 
			model.addAttribute("sblogs", sblogs);
			
		} catch (Exception e) {
			e.printStackTrace();
			return "error";
		}
		
		return "default";
	}
	
	/**
	 * @Title: queryPicnews
	 * @Description: 查询图片新闻
	 * @return String
	 */
	@RequestMapping(value="page_queryPicnews.action")
	public String queryPicnews(Picnews paramsPicnews,PaperUtil paperUtil,
			ModelMap model,HttpServletRequest request,HttpServletResponse response,HttpSession httpSession){
		try {
			//查询图片新闻
			Picnews picnews = indexManager.getPicnews(paramsPicnews); 
			
			model.addAttribute("picnews", picnews);
			
		} catch (Exception e) {
			e.printStackTrace();
			return "error";
		}
		
		return "tpxw";
	}
	
	/**
	 * @Title: listNewss
	 * @Description: 查询新闻资讯
	 * @return String
	 */
	@RequestMapping(value="page_listNewss.action")
	public String listNewss(News paramsNews,PaperUtil paperUtil,
			ModelMap model,HttpServletRequest request,HttpServletResponse response,HttpSession httpSession){
		try {
			//查询新闻资讯
			if (paramsNews==null) {
				paramsNews = new News();
			}
			paramsNews.setNews_flag(2);
			paperUtil.setPagination(paramsNews);
			int[] sum={0};
			List<News> newss = indexManager.listNewss(paramsNews,sum); 
			
			model.addAttribute("newss", newss);
			paperUtil.setTotalCount(sum[0]);
			
		} catch (Exception e) {
			e.printStackTrace();
			return "error";
		}
		
		return "news";
	}
	
	/**
	 * @Title: queryNews
	 * @Description: 查询新闻资讯详情
	 * @return String
	 */
	@RequestMapping(value="page_queryNews.action")
	public String queryNews(News paramsNews,PaperUtil paperUtil,
			ModelMap model,HttpServletRequest request,HttpServletResponse response,HttpSession httpSession){
		try {
			//查询新闻资讯详情
			News news = indexManager.getNews(paramsNews); 
			
			model.addAttribute("news", news);
			model.addAttribute("news_type", paramsNews.getNews_type());
			
		} catch (Exception e) {
			e.printStackTrace();
			return "error";
		}
		
		return "newsDetail";
	}

	/**
	 * @Title: listActivitys
	 * @Description: 查询活动公告
	 * @return String
	 */
	@RequestMapping(value="page_listActivitys.action")
	public String listActivitys(Activity paramsActivity,PaperUtil paperUtil,
			ModelMap model,HttpServletRequest request,HttpServletResponse response,HttpSession httpSession){
		try {
			//查询活动公告
			Activity activity = new Activity();
			activity.setActivity_flag(2);
			paperUtil.setPagination(activity);
			int[] sum={0};
			List<Activity> activitys = indexManager.listActivitys(activity,sum); 
			
			model.addAttribute("activitys", activitys);
			paperUtil.setTotalCount(sum[0]);
			
		} catch (Exception e) {
			e.printStackTrace();
			return "error";
		}
		
		return "activity";
	}
	
	/**
	 * @Title: queryActivity
	 * @Description: 查询活动公告详情
	 * @return String
	 */
	@RequestMapping(value="page_queryActivity.action")
	public String queryActivity(Activity paramsActivity,PaperUtil paperUtil,
			ModelMap model,HttpServletRequest request,HttpServletResponse response,HttpSession httpSession){
		try {
			//查询活动公告
			Activity activity = indexManager.getActivity(paramsActivity); 
			model.addAttribute("activity", activity);
		} catch (Exception e) {
			e.printStackTrace();
			return "error";
		}
		
		return "activityDetail";
	}
	
	/**
	 * @Title: listColleges
	 * @Description: 查询社团简介
	 * @return String
	 */
	@RequestMapping(value="page_listColleges.action")
	public String listColleges(College paramsCollege,PaperUtil paperUtil,
			ModelMap model,HttpServletRequest request,HttpServletResponse response,HttpSession httpSession){
		try {
			//查询社团简介
			if (paramsCollege==null) {
				paramsCollege = new College();
			}
			paramsCollege.setCollege_flag(2);
			paperUtil.setPagination(paramsCollege);
			int[] sum={0};
			List<College> colleges = indexManager.listColleges(paramsCollege,sum); 
			
			model.addAttribute("colleges", colleges);
			paperUtil.setTotalCount(sum[0]);
			
		} catch (Exception e) {
			e.printStackTrace();
			return "error";
		}
		
		return "college";
	}
	
	/**
	 * @Title: queryCollege
	 * @Description: 查询社团简介详情
	 * @return String
	 */
	@RequestMapping(value="page_queryCollege.action")
	public String queryCollege(College paramsCollege,PaperUtil paperUtil,
			ModelMap model,HttpServletRequest request,HttpServletResponse response,HttpSession httpSession){
		try {
			//查询社团简介详情
			College college = indexManager.getCollege(paramsCollege); 
			
			model.addAttribute("college", college);
		} catch (Exception e) {
			e.printStackTrace();
			return "error";
		}
		
		return "collegeDetail";
	}
	
	/**
	 * @Title: createCollegeShow
	 * @Description: 申请创建社团页面
	 * @return String
	 */
	@RequestMapping(value="page_createCollegeShow.action")
	public String createCollegeShow(){
		return "collegeCreate";
	}
	
	/**
	 * @Title: createCollege
	 * @Description: 申请创建社团
	 * @return String
	 */
	@RequestMapping(value="page_createCollege.action")
	@ResponseBody
	public JSONData createCollege(College paramsCollege,PaperUtil paperUtil,
			ModelMap model,HttpServletRequest request,HttpServletResponse response,HttpSession httpSession){
		JSONData jsonData = new JSONData();
		try {
			//检查社团名字是否已经存在
			College college = indexManager.getCollege(paramsCollege); 
			if (college!=null) {
				jsonData.setErrorReason("创建失败！该社团名字已经存在！");
				return jsonData;
			}
			//创建社团
			indexManager.createCollege(paramsCollege);
			
		} catch (Exception e) {
			e.printStackTrace();
			jsonData.setErrorReason("创建失败！后台服务器繁忙！");
			return jsonData;
		}
		return jsonData;
	}
	
	/**
	 * @Title: joinMemberShow
	 * @Description: 申请创建社团页面
	 * @return String
	 */
	@RequestMapping(value="page_joinMemberShow.action")
	public String joinMemberShow(College paramsCollege,PaperUtil paperUtil,
			ModelMap model,HttpServletRequest request,HttpServletResponse response,HttpSession httpSession){
		College college = indexManager.getCollege(paramsCollege); 
		model.addAttribute("college", college);
		
		return "memberJoin";
	}
	
	/**
	 * @Title: joinMember
	 * @Description: 申请加入社团
	 * @return String
	 */
	@RequestMapping(value="page_joinMember.action")
	@ResponseBody
	public JSONData joinMember(Member paramsMember,PaperUtil paperUtil,
			ModelMap model,HttpServletRequest request,HttpServletResponse response,HttpSession httpSession){
		JSONData jsonData = new JSONData();
		try {
			//检查社团是否已经加入
			Member member = indexManager.getMember(paramsMember); 
			if (member!=null) {
				if (member.getMember_flag()==1) {
					jsonData.setErrorReason("您的申请正在审批中，请等待！");
					return jsonData;
				}else {
					jsonData.setErrorReason("您已经是该社团成员，无需再次申请！");
					return jsonData;
				}
			}
			//申请加入社团
			indexManager.joinMember(paramsMember);
			
		} catch (Exception e) {
			e.printStackTrace();
			jsonData.setErrorReason("后台服务器异常！");
			return jsonData;
		}
		
		return jsonData;
	}
	
	/**
	 * @Title: myCollegeApply
	 * @Description: 查询创建社团申请
	 * @return String
	 */
	@RequestMapping(value="page_myCollegeApply.action")
	public String myCollegeApply(College paramsCollege,PaperUtil paperUtil,
			ModelMap model,HttpServletRequest request,HttpServletResponse response,HttpSession httpSession){
		try {
			//查询社团申请
			if (paramsCollege==null) {
				paramsCollege = new College();
			}
			//设置身份为当前用户
			User userFront = (User) httpSession.getAttribute("userFront");
			paramsCollege.setUser_id(userFront.getUser_id());
			//设置分页信息
			paperUtil.setPagination(paramsCollege);
			int[] sum={0};
			List<College> colleges = indexManager.listColleges(paramsCollege,sum); 
			
			model.addAttribute("colleges", colleges);
			paperUtil.setTotalCount(sum[0]);
			
		} catch (Exception e) {
			e.printStackTrace();
			return "error";
		}
		
		return "myCollegeApply";
	}
	
	/**
	 * @Title: myMemberApply
	 * @Description: 查询加入社团申请
	 * @return String
	 */
	@RequestMapping(value="page_myMemberApply.action")
	public String myMemberApply(Member paramsMember,PaperUtil paperUtil,
			ModelMap model,HttpServletRequest request,HttpServletResponse response,HttpSession httpSession){
		try {
			//查询加入社团申请
			if (paramsMember==null) {
				paramsMember = new Member();
			}
			//设置身份为当前用户
			User userFront = (User) httpSession.getAttribute("userFront");
			paramsMember.setUser_id(userFront.getUser_id());
			//设置分页信息
			paperUtil.setPagination(paramsMember);
			int[] sum={0};
			List<Member> members = indexManager.listMembers(paramsMember,sum); 
			
			model.addAttribute("members", members);
			paperUtil.setTotalCount(sum[0]);
			
		} catch (Exception e) {
			e.printStackTrace();
			return "error";
		}
		
		return "myMemberApply";
	}
	
	/**
	 * @Title: sblogs
	 * @Description: 留言板
	 * @return String
	 */
	@RequestMapping(value="page_listSblogs.action")
	public String listSblogs(Sblog paramsSblog,PaperUtil paperUtil,
			ModelMap model,HttpServletRequest request,HttpServletResponse response,HttpSession httpSession){
		try {
			//查询留言板
			if (paramsSblog==null) {
				paramsSblog = new Sblog();
			}
			paramsSblog.setSblog_flag(2);
			paperUtil.setPagination(paramsSblog);
			int[] sum={0};
			List<Sblog> sblogs = indexManager.listSblogs(paramsSblog,sum); 
			
			model.addAttribute("sblogs", sblogs);
			paperUtil.setTotalCount(sum[0]);
		} catch (Exception e) {
			e.printStackTrace();
			return "error";
		}
		
		return "sblog";
	}
	
	/**
	 * @Title: addSblog
	 * @Description: 新增留言
	 * @return String
	 */
	@RequestMapping(value="page_addSblog.action")
	@ResponseBody
	public JSONData addSblog(Sblog paramsSblog,PaperUtil paperUtil,
			ModelMap model,HttpServletRequest request,HttpServletResponse response,HttpSession httpSession){
		JSONData jsonData = new JSONData();
		try {
			//验证码验证
			String random = (String)httpSession.getAttribute("random");
			if (!random.equals(paramsSblog.getRandom())) {
				jsonData.setErrorReason("验证码错误！");
				return jsonData;
			}
			
			//新增留言
			indexManager.addSblog(paramsSblog);
			
		} catch (Exception e) {
			e.printStackTrace();
			jsonData.setErrorReason("后台服务器异常！");
			return jsonData;
		}
		
		return jsonData;
	}
	
	/**
	 * @Title: saveAdmin
	 * @Description: 保存修改个人信息
	 * @return String
	 */
	@RequestMapping(value="page_saveUserFront.action",method=RequestMethod.POST)
	@ResponseBody
	public JSONData saveUserFront(User paramsUser,
			ModelMap model,HttpServletRequest request,HttpServletResponse response,HttpSession httpSession){
		JSONData jsonData = new JSONData();
		try {
			 //保存修改个人信息
			indexManager.updateUser(paramsUser);
			//更新session
			User admin = new User();
			admin.setUser_id(paramsUser.getUser_id());
			admin = indexManager.getUser(admin);
			httpSession.setAttribute("userFront", admin);
		} catch (Exception e) {
			e.printStackTrace();
			jsonData.setErrorReason("后台服务器异常");
			return jsonData;
		}
		return jsonData;
	}
	
	/**
	 * @Title: saveUserFrontPass
	 * @Description: 保存修改个人密码
	 * @return String
	 */
	@RequestMapping(value="page_saveUserFrontPass.action",method=RequestMethod.POST)
	@ResponseBody
	public JSONData saveUserFrontPass(User paramsUser,
			ModelMap model,HttpServletRequest request,HttpServletResponse response,HttpSession httpSession){
		JSONData jsonData = new JSONData();
		try {
			 //保存修改个人信息
			indexManager.updateUser(paramsUser);
			//更新session
			User userFront = (User)httpSession.getAttribute("userFront");
			if (userFront!=null) {
				userFront.setUser_pass(paramsUser.getUser_pass());
				httpSession.setAttribute("userFront", userFront);
			}
		} catch (Exception e) {
			e.printStackTrace();
			jsonData.setErrorReason("后台服务器异常");
			return jsonData;
		}
		return jsonData;
	}
	
	/**
	 * @Title: reg
	 * @Description: 跳转注册页面
	 * @return String
	 */
	@RequestMapping(value="page_reg.action")
	public String reg(){
		return "reg";
	}
	
	/**
	 * @Title: myInfo
	 * @Description: 跳转个人信息页面
	 * @return String
	 */
	@RequestMapping(value="page_myInfo.action")
	public String myInfo(){
		return "myInfo";
	}
	
	/**
	 * @Title: myPwd
	 * @Description: 跳转个人密码页面
	 * @return String
	 */
	@RequestMapping(value="page_myPwd.action")
	public String myPwd(){
		return "myPwd";
	}
	
	public String getTip() {
		return tip;
	}

	public void setTip(String tip) {
		this.tip = tip;
	}
	

}
